<?php

/**
 * @file
 *   Page callbacks for the ajaxchatbox module.
 */

/**
 * Admin settings form
 */
function ajaxchatbox_admin_settings($form, &$form_state) {
  $form['display_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
    '#collapsible' => TRUE,
  );
  $form['display_settings']['ajaxchatbox_max_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum amount of characters in a message'),
    '#default_value' => variable_get('ajaxchatbox_max_length', 255),
    '#size' => 5,
    '#required' => TRUE,
    '#description' => t('Set the amount of allowed characters per message. Enter 0 for no limit. This will not affect existing messages.'),
  );
  $form['display_settings']['ajaxchatbox_showamount_block'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of messages to show in the block'),
    '#default_value' => variable_get('ajaxchatbox_showamount_block', 10),
    '#size' => 4,
    '#maxlength' => 4,
    '#required' => TRUE,
    '#description' => t('Set the number of ajaxchatbox messages to show in the block.'),
  );
  $form['display_settings']['ajaxchatbox_showamount_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of messages to show on the page'),
    '#default_value' => variable_get('ajaxchatbox_showamount_page', 30),
    '#size' => 4,
    '#maxlength' => 4,
    '#required' => TRUE,
    '#description' => t('Set the number of ajaxchatbox messages to show on the page.'),
  );
  $form['display_settings']['ajaxchatbox_ascending'] = array(
    '#type' => 'checkbox',
    '#title' => t('Post newest messages on top'),
    '#default_value' => variable_get('ajaxchatbox_ascending', 1),
    '#description' => t('When checked, new messages will appear on the top instead of the bottom.'),
  );
  $form['display_settings']['ajaxchatbox_widget_type'] = array(
    '#type' => 'radios',
    '#title' => t('Input form type'),
    '#default_value' => variable_get('ajaxchatbox_widget_type', 'textfield'),
    '#description' => t('Choose the type of form widget used for entering a shout.'),
    '#options' => array(
      'textfield' => t('Textfield'),
      'textarea' => t('Textarea'),
    ),
  );
  $form['display_settings']['ajaxchatbox_time_format'] = array(
    '#type' => 'select',
    '#title' => t('Time format'),
    '#default_value' => variable_get('ajaxchatbox_time_format', 'ago'),
    '#description' => t('Choose the format which shout times will be rendered in.'),
    '#options' => array(
      'ago' => t('Time ago (1 hour 15 minutes ago)'),
      'small' => t('Small !time', array('!time' => '(' . format_date(REQUEST_TIME, 'short') . ')')),
      'medium' => t('Medium !time', array('!time' => '(' . format_date(REQUEST_TIME, 'medium') . ')')),
      'large' => t('Large !time', array('!time' => '(' . format_date(REQUEST_TIME, 'long') . ')')),
    ),
  );
  $form['display_settings']['ajaxchatbox_restrict_general_messages'] = array(
    '#type' => 'checkbox',
    '#title' => t('Restrict general messages'),
    '#default_value' => variable_get('ajaxchatbox_restrict_general_messages', 1),
    '#description' => t('When checked, ajaxchatboxes outside of specific contexts will be restricted to general messages. There are modules that utilize the ajaxchatbox API, such as ajaxchatbox group, that will enter messages meant to only be displayed in certain locations. Checking this will insure that those messages are only displayed by the modules responsible for creating them.'),
  );

  // Generate choices for profile fields
  if (module_exists('profile')) {
    // TODO: Make this work in D7
    $choices = array();
    $fields = db_query("SELECT name FROM {profile_fields} ORDER BY name ASC");
    while ($field = db_fetch_object($fields)) {
      $choices[$field->name] = $field->name;
    }

    $form['display_settings']['ajaxchatbox_profile_name'] = array(
      '#type' => 'select',
      '#title' => t('Use profile field for user name'),
      '#options' => array(0 => '- None -') + $choices,
      '#default_value' => variable_get('ajaxchatbox_profile_name', 0),
      '#description' => t('Select an existing custom profile field to use as the user name.'),
    );
  }
  else {
    $form['display_settings']['ajaxchatbox_profile_name'] = array(
      '#type' => 'item',
      '#title' => t('Use profile field for user name'),
      '#description' => t('Enable the profile module to allow ajaxchatbox to use the user\'s real name instead of the username.'),
    );
  }

  // TODO: Figure out how to create a form for filter
  /* $form['ajaxchatbox_filter_format'] = _ajaxchatbox_filter_form(); */
  /* $form['ajaxchatbox_filter_format']['#collapsed'] = FALSE; */
  /* $form['ajaxchatbox_filter_format']['ajaxchatbox_escape_html'] = array( */
  /*   '#type' => 'checkbox', */
  /*   '#title' => t('Escape all HTML & formatting'), */
  /*   '#default_value' => variable_get('ajaxchatbox_escape_html', 1), */
  /*   '#description' => '<strong>' . t('Checking this will ignore the input format selected below, and escape all HTML from the messages.') . '</strong>', */
  /*   '#weight' => -10, */
  /* ); */

  $form['time_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Time settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['time_settings']['ajaxchatbox_refresh'] = array(
    '#type' => 'textfield',
    '#title' => t('Auto refresh (in seconds)'),
    '#default_value' => variable_get('ajaxchatbox_refresh', 20),
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('ajaxchatbox can be set to automatically refresh every x number of seconds.  Set to 0 to turn off the auto refresh.'),
  );
  $form['time_settings']['ajaxchatbox_anonymous_timeout'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of minutes for which anonymous users may edit or delete their own messages'),
    '#default_value' => variable_get('ajaxchatbox_anonymous_timeout', 20),
    '#size' => 4,
    '#maxlength' => 4,
    '#description' => t('Anonymous users can edit or delete their post within this amount of time from it being posted, as long as they have the same IP address as when they posted it.  If you don\'t want shout editing and/or deleting, remove these permissions from Drupal\'s anonymous users role. Set 0 for no limit.'),
  );
  $form['time_settings']['ajaxchatbox_registered_timeout'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of minutes for which registered users may edit or delete their own messages'),
    '#default_value' => variable_get('ajaxchatbox_registered_timeout', 0),
    '#size' => 4,
    '#maxlength' => 4,
    '#description' => t('Registered users can edit or delete their post within this amount of time from it being posted.  If you don\'t want editing and/or deleting, remove these permissions from Drupal\'s authenticated users role. Set 0 for no limit.'),
  );
  $form['time_settings']['ajaxchatbox_expire'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of seconds after which messages will be purged from the database'),
    '#default_value' => variable_get('ajaxchatbox_expire', 0),
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('Messages will be permanently deleted after the number of seconds specified.  Messages will never expire when this is set to 0.'),
  );
  $form['security_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Security settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['security_settings']['ajaxchatbox_amount_messages_blocked'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of messages to block user'),
    '#default_value' => variable_get('ajaxchatbox_amount_messages_blocked', 5),
    '#size' => 4,
    '#maxlength' => 4,
    '#required' => TRUE,
    '#description' => t('Set the number of ajaxchatbox messages to block user.'),
  );
  return system_settings_form($form);
}

/**
 * Validate the admin settings form
 */
function ajaxchatbox_admin_settings_validate($form, &$form_state) {
  if (!is_numeric($form_state['values']['ajaxchatbox_showamount_block'])) {
    form_set_error('ajaxchatbox_showamount_block', t('Block amount must be numeric'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_showamount_page'])) {
    form_set_error('ajaxchatbox_showamount_page', t('Page amount must be numeric'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_max_length'])) {
    form_set_error('ajaxchatbox_max_length', t('ajaxchatbox length must be numeric'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_refresh'])) {
    form_set_error('ajaxchatbox_refresh', t('Refresh setting must be a numeric value'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_anonymous_timeout'])) {
    form_set_error('ajaxchatbox_anonymous_timeout', t('Anonymous timeout setting must be a numeric value'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_registered_timeout'])) {
    form_set_error('ajaxchatbox_registered_timeout', t('Registered timeout setting must be a numeric value'));
  }

  if (!is_numeric($form_state['values']['ajaxchatbox_expire'])) {
    form_set_error('ajaxchatbox_expire', t('Expiration setting must be a numeric value'));
  }
}